{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Placeholders\n",
    "\n",
    "We introduce how to use placeholders in TensorFlow.\n",
    "\n",
    "First we import the necessary libraries and reset the graph session."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tensorflow.python.framework import ops\n",
    "ops.reset_default_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Start graph session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "sess = tf.Session()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Declare a placeholder\n",
    "\n",
    "We declare a placeholder by using TensorFlow's function, `tf.placeholder()`, which accepts a data-type argument (`tf.float32`) and a shape argument, `(4,4)`.  Note that the shape can be a tuple or a list, `[4,4]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "x = tf.placeholder(tf.float32, shape=(4, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "For illustration on how to use the placeholder, we create input data for it and an operation we can visualize on Tensorboard.\n",
    "\n",
    "Note the useage of `feed_dict`, where we feed in the value of x into the computational graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.1175806   0.88121527  0.00815445  0.93555111]\n",
      " [ 0.97369134  0.14595009  0.16398087  0.76570976]\n",
      " [ 0.67633879  0.11748746  0.01266815  0.32564184]\n",
      " [ 0.99007022  0.6825515   0.54524553  0.01503101]]\n"
     ]
    }
   ],
   "source": [
    "# Input data to placeholder, note that 'rand_array' and 'x' are the same shape.\n",
    "rand_array = np.random.rand(4, 4)\n",
    "\n",
    "# Create a Tensor to perform an operation (here, y will be equal to x, a 4x4 matrix)\n",
    "y = tf.identity(x)\n",
    "\n",
    "# Print the output, feeding the value of x into the computational graph\n",
    "print(sess.run(y, feed_dict={x: rand_array}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "To visualize this in Tensorboard, we merge summaries and write to a log file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "merged = tf.summary.merge_all()\n",
    "writer = tf.summary.FileWriter(\"/tmp/variable_logs\", sess.graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "We run the following command at the prompt:\n",
    "\n",
    "`tensorboard --logdir=/tmp`\n",
    "\n",
    "Which will tell us where to navigate chrome to to visualize the computational graph.  Default is\n",
    "\n",
    "`http://0.0.0.0:6006/`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "![Placeholders_in_Tensorboard](https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/images/03_placeholder.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
